package com.mesnac.comm.log;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;

import com.mesnac.comm.config.LoggerConfig;
import com.mesnac.comm.session.SysContent;
import com.mesnac.user.entity.User;

public class LogAdvice implements MethodBeforeAdvice {
	public final Log logger = LogFactory.getLog(this.getClass());

	public void before(Method m, Object[] args, Object target) throws Throwable{
		System.out.println("[系统日志]"+new Date()+m.getName()+Arrays.toString(args)+target.getClass());
		//根据操作的方法来决定是否保存日志
		String userid = "";
		if(((User)(HttpSession)SysContent.getRequest().getAttribute("user"))!=null)
			userid = ((User)(HttpSession)SysContent.getRequest().getAttribute("user")).getUserid();
		if(LoggerConfig.getMethodStr(m.getName())!=m.getName()){
			String mname = LoggerConfig.getMethodStr(m.getName());
			for (Object object : args) {
				
				if(object.getClass().equals(User.class)){
					//logger.info(arg0)
					String methodName = LoggerConfig.getMethodStr(m.getName());
					System.out.println("[系统日志]"+((User)object).getUserid()+methodName);
				}
				if(object.getClass().equals(String.class)){
					System.out.println("[系统日志]"+userid+mname+(String)object);
				}
				/*
				if(object.getClass().equals(Bookingm.class)){
					System.out.println("[系统日志]"+userid+"操作:"+mname+" 凭证号:"+((Bookingm)object).getTrid());
				}
				if(object.getClass().equals(Integer.class)){
					System.out.println("[系统日志]"+userid+"操作:"+mname+" 凭证号:"+object);
				}
				*/
			}
		}

		
		
		logger.info("test log");
	}
	
	private String getMethodString(String method){
		String methodStr = "";		
		return methodStr;
	}
}
